package com.zige.zige.bj_vr;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.SeekBar;

import com.zige.zige.R;
import com.zige.zige.view.panoplayer.VideoStateListener;

import com.zige.zige.view.panoplayer.PanoPlayerView;

/**
 * Created by admin on 16/1/9.
 */
public class VrPlayerView extends RelativeLayout implements VideoStateListener {

    private ImageButton mBtnPlay;
    private ImageButton mBtnPause;
    private Button mBtnControlMode;
    private Button mBtnDisplayMode;
    private SeekBar mSeekBar;
    private FullscreenCallback mFullscreenCallbackImpl;
    private PanoPlayerView mPanoView;

    public VrPlayerView(Context context) {
        super(context);
        initView(context);
    }

    public VrPlayerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public VrPlayerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public void setFullscreenCallback(FullscreenCallback mFullscreenCallbackImpl) {
        this.mFullscreenCallbackImpl = mFullscreenCallbackImpl;
    }

    private void initView(Context context) {
        View.inflate(context, R.layout.player_view, this);

        mPanoView = (PanoPlayerView)findViewById(R.id.pano_view);
        mBtnPlay = (ImageButton)findViewById(R.id.btn_play);
        mBtnPause = (ImageButton)findViewById(R.id.btn_pause);
        mBtnControlMode = (Button)findViewById(R.id.btn_control_mode);
        mBtnDisplayMode = (Button)findViewById(R.id.btn_display_mode);
        Button mBtnFullscreen = (Button) findViewById(R.id.btn_fullscreen);
        mSeekBar = (SeekBar)findViewById(R.id.seek_bar);

        mBtnPlay.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == mPanoView) return;
                mPanoView.getVideoPlayer().play();
                mBtnPlay.setVisibility(GONE);
                mBtnPause.setVisibility(VISIBLE);

            }
        });

        mBtnPause.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null == mPanoView) return;
                mPanoView.getVideoPlayer().pause();
                mBtnPause.setVisibility(GONE);
                mBtnPlay.setVisibility(VISIBLE);


            }
        });

        mBtnControlMode.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mPanoView.setGyroControlEnabled(!mPanoView.isGyroControlEnabled());
            }
        });
        mBtnDisplayMode.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                mPanoView.setVrModeEnabled(!mPanoView.isVrModeEnabled());
            }
        });

        mBtnFullscreen.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (null != mFullscreenCallbackImpl) {
                    mFullscreenCallbackImpl.toggleFullscreen();
                }
            }
        });
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (fromUser) {
                    mPanoView.getVideoPlayer().seekToPercentage(progress / 100.0);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                mPanoView.getVideoPlayer().beforeSeek();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mPanoView.getVideoPlayer().seekToPercentage(seekBar.getProgress() / 100.0);
                mPanoView.getVideoPlayer().afterSeek();
            }
        });

        mPanoView.getVideoPlayer().setVideoStateListener(this);

    }

    @Override
    public void onPrepared(boolean succeed) {
        if (succeed) {
            mBtnPlay.setEnabled(true);
        }
    }

    @Override
    public void onStarted() {
        mBtnPlay.setVisibility(GONE);
        mBtnPause.setVisibility(VISIBLE);
    }

    @Override
    public void onPaused(boolean needBuffer) {
        mBtnPause.setVisibility(GONE);
        mBtnPlay.setVisibility(VISIBLE);
    }

    @Override
    public void onResumed() {
        mBtnPlay.setVisibility(GONE);
        mBtnPause.setVisibility(VISIBLE);
    }

    @Override
    public void onProgress(float currentPercentge, float loadedPercentage) {
        mSeekBar.setProgress((int) (currentPercentge * 100));
        mSeekBar.setSecondaryProgress((int) (loadedPercentage * 100));
    }

    @Override
    public void onStopped() {
        mBtnPause.setVisibility(GONE);
        mBtnPlay.setVisibility(VISIBLE);
    }

    public interface FullscreenCallback {
        void toggleFullscreen();
    }

}